package com.ruoyi.project.goods.bill.controller;

import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.repertory.domain.GoodsRepertory;
import com.ruoyi.project.repertory.service.IGoodsRepertoryService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.goods.bill.domain.GoodsBill;
import com.ruoyi.project.goods.bill.service.IGoodsBillService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 发货管理Controller
 * 
 * @author lonsn
 * @date 2020-11-04
 */
@Controller
@RequestMapping("/goods/bill")
public class GoodsBillController extends BaseController
{
    private String prefix = "goods/bill";

    @Autowired
    private IGoodsBillService goodsBillService;
    @Autowired
    private IGoodsRepertoryService goodsRepertoryService;

    @RequiresPermissions("goods:bill:view")
    @GetMapping()
    public String bill()
    {
        return prefix + "/bill";
    }

    @RequiresPermissions("goods:bill:view:admin")
    @GetMapping("/billAdmin")
    public String billAdmin()
    {
        return prefix + "/billAdmin";
    }

    /**
     * 查询发货管理列表
     */
    @RequiresPermissions("goods:bill:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsBill goodsBill)
    {
        startPage();
        //如果是总店，可以查询所有出入库
        if(ShiroUtils.getSysUser().getDeptId()!=100){
            goodsBill.setDeptId(ShiroUtils.getSysUser().getDeptId());
        }

        List<GoodsBill> list = goodsBillService.selectGoodsBillList(goodsBill);
        return getDataTable(list);
    }

    /**
     * 导出发货管理列表
     */
    @RequiresPermissions("goods:bill:export")
    @Log(title = "发货管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsBill goodsBill)
    {
        List<GoodsBill> list = goodsBillService.selectGoodsBillList(goodsBill);
        ExcelUtil<GoodsBill> util = new ExcelUtil<GoodsBill>(GoodsBill.class);
        return util.exportExcel(list, "bill");
    }

    /**
     * 新增发货管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存发货管理
     */
    @RequiresPermissions("goods:bill:add")
    @Log(title = "发货管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsBill goodsBill)
    {
        return toAjax(goodsBillService.insertGoodsBill(goodsBill));
    }

    /**
     * 修改发货管理
     */
    @GetMapping("/edit/{billNo}")
    public String edit(@PathVariable("billNo") String billNo, ModelMap mmap)
    {
        GoodsBill goodsBill = goodsBillService.selectGoodsBillById(billNo);
        mmap.put("goodsBill", goodsBill);
        return prefix + "/edit";
    }

    /**
     * 修改保存发货管理
     */
    @RequiresPermissions("goods:bill:edit")
    @Log(title = "发货管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsBill goodsBill)
    {
        return toAjax(goodsBillService.updateGoodsBill(goodsBill));
    }

    /**
     * 删除发货管理
     */
    @RequiresPermissions("goods:bill:remove")
    @Log(title = "发货管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(goodsBillService.deleteGoodsBillByIds(ids));
    }


    @RequiresPermissions("goods:bill:bill_audit")
    @GetMapping("/bill_audit")
    public String billAudit()
    {
        return prefix + "/bill_audit";
    }






    /**
     * 发货单审核
     */
    @RequiresPermissions("goods:bill:audit")
    @Log(title = "发货单审核", businessType = BusinessType.UPDATE)
    @PostMapping( "/audit")
    @ResponseBody
    public AjaxResult audit(String billNo,Integer status)
    {
//        GoodsBill formGoodBill=new GoodsBill();
//        formGoodBill.setBillNo(billNo);
//        formGoodBill.setStatus(status);
        int rows=0;
        GoodsBill goodsBill= goodsBillService.selectGoodsBillById(billNo);
        goodsBill.setCreateBy(ShiroUtils.getLoginName());
        if(status==3){
            //入库
            if(goodsBill.getStatus()==2){
                rows=goodsBillService.auditGoodsBill(goodsBill);
            }
        }else if(status==2){
            //发货
            if(goodsBill.getStatus()==1){
                rows=goodsBillService.auditGoodsBill(goodsBill);
            }
        }
        return toAjax(rows);
    }
}
